package com.shms.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfLogin;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShLogin;
import com.shms.service.ShLoginService;

/**
 * 文件名：LoginAction.java
 * 编写者：杨勇
 * 创建时间：2013-3-11
 * 简要描述：
 * 后台登录的Action文件
 */
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 6149804190146015897L;
	private GroupOfLogin groupOfLogin;
	private ShLoginService shLoginService;
	private ShLogin shLogin;
	//登录失败的提示信息参数
	private String message;
	private Integer[] nos;
	
	public String addLogin() {
		return SUCCESS;
	}
	
	public String queryByLoginId() {
		//获取要查询的单条记录id
		long id = Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		ShLogin shLoginParams = new ShLogin();
		shLoginParams.setLid(id);
		shLogin = shLoginService.queryById(shLoginParams);
		return SUCCESS;
	}
	
	/**
	 * 保存登录账号信息
	 * @return
	 */
	public String saveLogin() {
		boolean rtn = shLoginService.saveLogin(shLogin);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		return SUCCESS;
	}
	
	/**
	 * 根据登录信息查询
	 * @return SUCCESS
	 */
	public String queryLoginByExample() {
		List<ShLogin> list = shLoginService.queryLoginByExample(shLogin);
		if (list == null || list.size() != 1) {
			message = "用户名或密码错误!";
			return INPUT;
		} else {
			return SUCCESS;
		} 
	}
	
	/**
	 * 根据分页信息，查询登陆账号信息列表
	 * @return 
	 */
	public String queryLogin() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询动态信息的分页信息输入参数
		GroupOfLogin groupOfLoginParams = new GroupOfLogin();
		groupOfLoginParams.setCurrentPage(currentPage);
		groupOfLoginParams.setType(type);
		groupOfLoginParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfLogin = shLoginService.queryLogin(groupOfLoginParams);
		if (groupOfLogin.getShLogin() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	
	/**
	 * 批量删除登录用户信息
	 * @return 
	 */
	public String deleteLogin() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shLoginService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage = groupOfLogin.getCurrentPage() - 1;
			//获取传来的上一页或下一页标志
			String type = groupOfLogin.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfLogin groupOfLoginParams = new GroupOfLogin();
			groupOfLoginParams.setCurrentPage(currentPage);
			groupOfLoginParams.setType(type);
			groupOfLoginParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfLogin = shLoginService.queryLogin(groupOfLoginParams);
			return SUCCESS;
		}else{
			message = "删除动态信息失败";
			return INPUT;
		}
	}
	
	public ShLoginService getShLoginService() {
		return shLoginService;
	}
	
	public void setShLoginService(ShLoginService shLoginService) {
		this.shLoginService = shLoginService;
	}

	public ShLogin getShLogin() {
		return shLogin;
	}

	public void setShLogin(ShLogin shLogin) {
		this.shLogin = shLogin;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public GroupOfLogin getGroupOfLogin() {
		return groupOfLogin;
	}

	public void setGroupOfLogin(GroupOfLogin groupOfLogin) {
		this.groupOfLogin = groupOfLogin;
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
}
